<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/12/21
 * Time: 11:27
 */
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING); //关闭警告
require_once "../extend/Mail.php"; //引入邮箱配置
use Mail\Mail;  //使用邮箱配置中类

    //接收前端数据
    $to = $_POST['email'];
    $username = $_POST['username']!=""?$_POST['username']:"WT18-匿名用户";
    $range_code=random(4,1);
    $code = $range_code=random(4,1)!=""?    $range_code=random(4,1):"8750";
    //简单的表单验证

  if ($to !=""){
        $params = [
             "to"=>$to,
             "title"=>"WT18-JG-验证码",
             "data"=>['username'=>$username,'code'=>$code]
        ];
        $result = Mail::sendMail($params);
        if(!$result){
            $arr = ['code'=>100,"msg"=>"邮件发送失败~","data"=>"$to $username $code $range_code"];
        }else{
            $arr = ['code'=>200,"msg"=>"发送成功~","data"=>"","info"=>"请尽快登陆邮箱查询验证码完成注册！\n验证码有效时间为5分钟！"];
        }
  }else{
      $arr = ['code'=>100,"msg"=>"参数缺少~" ,"data"=>""];
  }

    $json = json_encode($arr,JSON_UNESCAPED_UNICODE);
    echo $json;

function random($length = 6 , $numeric = 0) {
    PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
    if($numeric) {
        $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
    } else {
        $hash = '';
        $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
        $max = strlen($chars) - 1;
        for($i = 0; $i < $length; $i++) {
            $hash .= $chars[mt_rand(0, $max)];
        }
    }
    return $hash;
}